package Demoo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo1 {

    ServerSocket serverSocketsocket = null;

    public Demo1 (int p) throws IOException {
        serverSocketsocket = new ServerSocket(p);
    }

    public void start() throws IOException {
        System.out.println("服务器上线");
        ExecutorService executorService = Executors.newCachedThreadPool();
        while (true) {
            Socket socket = serverSocketsocket.accept();
            executorService.submit(() -> {
                poccess(socket);
            });

        }
    }

    private void poccess(Socket socket) {
        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()) {
            Scanner sc = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            while (true) {
                if (!sc.hasNext()) {
                    System.out.println("客服端下线");
                    break;
                }
                String s1 = sc.next();
                String s2 = poccessnet(s1);
                printWriter.println(s2);
                printWriter.flush();
                System.out.printf("[%s:%d] req:%s resp:%s",socket.getInetAddress(), socket.getPort(), s1, s2);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private String poccessnet(String s1) {
        return s1;
    }

    public static void main(String[] args) throws IOException {
        Demo1 demo1 = new Demo1(9099);
        demo1.start();
    }
}
